package com.hbxy.favorite.service.impl;

import com.hbxy.favorite.dao.CommentMapper;
import com.hbxy.favorite.dao.UserMapper;
import com.hbxy.favorite.domain.Comment;
import com.hbxy.favorite.domain.CommentExample;
import com.hbxy.favorite.domain.User;
import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.CommentService;
import com.hbxy.favorite.utils.RegUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public ResponseBo addComment(Comment comment) {

        if(RegUtil.isNotNull(comment.getCollectId()) && RegUtil.isNotNull(comment.getSendUserId())){

            if(RegUtil.isNotNull(comment.getContent())){

                User user = userMapper.selectByPrimaryKey(comment.getSendUserId());
                comment.setSendUserName(user.getUserName());
                comment.setCreateTime(new Date());
                commentMapper.insert(comment);

                CommentExample example =new CommentExample();
                CommentExample.Criteria criteria = example.createCriteria();
                criteria.andCollectIdEqualTo(comment.getCollectId());
                List<Comment> commentList =commentMapper.selectByExample(example);


                return  ResponseBo.ok(commentList);
            }
            else
                return ResponseBo.error("请输入评论内容！");
        }
        else

            return ResponseBo.error("缺少参数！");
    }
}
